package com.huazi.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huazi.commonutils.CommentResult;
import com.huazi.eduservice.entity.EduChapter;
import com.huazi.eduservice.entity.EduVideo;
import com.huazi.eduservice.mapper.EduChapterMapper;
import com.huazi.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huazi.eduservice.service.EduVideoService;
import com.huazi.eduservice.vo.ChapterVideo;
import com.huazi.eduservice.vo.Options;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author huazi
 * @since 2021-03-18
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    EduVideoService eduVideoService;
    @Override
    public List<Options> getChapter(String courseId) {
        List<EduChapter> eduChapters = baseMapper.selectList(new QueryWrapper<EduChapter>().eq("course_id", courseId));
        List<Options> options= new ArrayList<>();
        if(eduChapters!=null){
            for (EduChapter cha:eduChapters
            ) {
                Options opt=new Options();
                //章节id和标题赋值
                opt.setId(cha.getId()).setLabel(cha.getTitle());
                //根据章节id获取所有小节
                List<Options> options1 = getOptions(cha.getId());
                if(options1!=null) opt.setChildren(options1);
                options.add(opt);
            }
            return options;
        }
        return null;
    }

    @Override
    public List<ChapterVideo> getChapterVideo(String courseId) {
        List<EduChapter> eduChapters = baseMapper.selectList(new QueryWrapper<EduChapter>().eq("course_id", courseId));
        List<ChapterVideo> chapterVideos= new ArrayList<>();
//        System.out.println(eduChapters);
        if(eduChapters!=null){
            for (EduChapter cha:eduChapters
            ) {
                ChapterVideo cv=new ChapterVideo();
                //章节id和标题赋值
                cv.setChapterId(cha.getId()).setTitle(cha.getTitle());
                //根据章节id获取所有小节
                List<EduVideo> videos = eduVideoService.list(new QueryWrapper<EduVideo>().eq("chapter_id", cv.getChapterId()));
                cv.setVideos(videos);
                chapterVideos.add(cv);
            }
            return chapterVideos;
        }
        return chapterVideos;
    }

    //    根据章节id获取所有的小节
    public List<Options> getOptions(String chapterId){
        List<EduVideo> eduVideos = eduVideoService.list(new QueryWrapper<EduVideo>().eq("chapter_id", chapterId));
        if(eduVideos!=null&&eduVideos.size()>0) {//循环添加该章节的小节
            List<Options> list=new ArrayList<>();
            for (EduVideo video :eduVideos
            ) {
                Options options=new Options();
                options.setId(video.getId()).setLabel(video.getTitle());
                list.add(options);
            }
            return list;
        }
        return null;
    }
}
